﻿/*
   @Description:agency中签约模块js
   @Author:ZB,YPL
   @Date:2014-04-16 
 */

var OnlyTrust = {};
var PopupWindow = {};

//查看合同书事件
OnlyTrust.appendShowContractBookClickEvent = function (selector) {
    $(selector).next().next().show();
    $(selector).parent().siblings().find("ul").hide();
};

//隐藏合同书事件
OnlyTrust.appendHideContractBookClickEvent = function (selector) {
    $(selector).parent().parent().hide();
};

//删除合同书
OnlyTrust.delContractBookClickEvent = function (selector) {
    $(selector).parent().remove();
};

//委托类型切换
PopupWindow.changeOnlyTrustType = function (element) {


    if ($(element).attr('onlytype') == '3') {
        //选中反签
        $(element).find('input').attr('checked', 'checked');

        $("#divDeposit").show();
        $("#emrequired").hide();

        $("#divDeposit").find("input").each(function () {
            $(this).val($(this).attr("mydeposit"));
        });
        agencyVerify.nonnegativeInput("input[deposit=deposit]");
    }

    if ($(element).attr('onlytype') == '2') {
        //选中上钱委托
        $(element).find('input').attr('checked', 'checked');
        $("#divDeposit").show();
        $("#emrequired").show();
        $("#divDeposit").find("input").each(function () {
            $(this).val($(this).attr("mydeposit"));
        });
        agencyVerify.nonnegativeInput("input[deposit=deposit]");
    }
    if ($(element).attr('onlytype') == '1') {
        //选中未上钱委托
        $(element).find('input').attr('checked', 'checked');
        if ($("#divDeposit").find("input").length > 0) {
            $("#divDeposit").find("input").each(function () {
                $(this).attr("mydeposit", $(this).val());
            });
            $("#divDepositModel").html($("#divDeposit").html());
            $("#divDeposit").hide();
            $("#emrequired").hide();
        }
    }


};


//检测委托类型数据
PopupWindow.checkTypeDataValue = true;
PopupWindow.checkTypeData = function () {
    var currentType = $('#divOnlyTrustType input[name=OnlyTrustType]:checked');
    if ($(currentType).val() == '1') {
        if (!$('#btnPopupTrustsTime1').val()) {
            $.OKDialogAgency("起始日期不能为空！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        }
        if (!$('#btnPopupTrustsTime2').val()) {
            $.OKDialogAgency("结束日期不能为空！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        }
        if (!$('#btnTrustBookNo').val()) {
            $.OKDialogAgency("委托书编号不能为空！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        }
        if (!$('#btnRegisterPerson').val()) {
            $.OKDialogAgency("签署人不能为空！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        }
        if (!$('#hidUserId').val()) {
            $.OKDialogAgency("签署人请从下拉框选取！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        }
        //if ($('#uploadContractBookDiv').html() == false) {
        //    $.OKDialogAgency("至少上传1个附件！");
        //    PopupWindow.checkTypeDataValue = false;
        //    return false;
        //}
        if ($('#uploadContractBookDiv').find('div[attachmentName]').length > 10) {
            $.OKDialogAgency("至多上传10个附件！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        }
        if ($("#spanAddContacts a").length > 5) {
            $.OKDialogAgency("联系人最多添加5个！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        };
        PopupWindow.checkTypeDataValue = true;
    }
    if ($(currentType).val() == '2') {
        if (!$('#btnPopupTrustsTime1').val()) {
            $.OKDialogAgency("起始日期不能为空！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        }
        if (!$('#btnPopupTrustsTime2').val()) {
            $.OKDialogAgency("结束日期不能为空！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        }
        if (!$("#divDeposit input[deposit=deposit]").val()) {
            $.OKDialogAgency("返佣金额不能为空！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        }
        if (!$('#btnTrustBookNo').val()) {
            $.OKDialogAgency("委托书编号不能为空！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        }
        if (!$('#btnRegisterPerson').val()) {
            $.OKDialogAgency("签署人不能为空！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        }
        if (!$('#hidUserId').val()) {
            $.OKDialogAgency("签署人请从下拉框选取！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        }
        //if ($('#uploadContractBookDiv').html() == false) {
        //    $.OKDialogAgency("至少上传1个附件！");
        //    PopupWindow.checkTypeDataValue = false;
        //    return false;
        //}
        if ($('#uploadContractBookDiv').find('div[attachmentName]').length > 10) {
            $.OKDialogAgency("至多上传10个附件！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        }
        if ($("#spanAddContacts a").length > 5) {
            $.OKDialogAgency("联系人最多添加5个！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        };
        PopupWindow.checkTypeDataValue = true;
    }

    if ($(currentType).val() == '3') {
        if (!$('#btnPopupTrustsTime1').val()) {
            $.OKDialogAgency("起始日期不能为空！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        }
        if (!$('#btnPopupTrustsTime2').val()) {
            $.OKDialogAgency("结束日期不能为空！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        }

        if (!$('#btnTrustBookNo').val()) {
            $.OKDialogAgency("委托书编号不能为空！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        }
        if (!$('#btnRegisterPerson').val()) {
            $.OKDialogAgency("签署人不能为空！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        }
        if (!$('#hidUserId').val()) {
            $.OKDialogAgency("签署人请从下拉框选取！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        }
        //if ($('#uploadContractBookDiv').html() == false) {
        //    $.OKDialogAgency("至少上传1个附件！");
        //    PopupWindow.checkTypeDataValue = false;
        //    return false;
        //}
        if ($('#uploadContractBookDiv').find('div[attachmentName]').length > 10) {
            $.OKDialogAgency("至多上传10个附件！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        }
        if ($("#spanAddContacts a").length > 5) {
            $.OKDialogAgency("联系人最多添加5个！");
            PopupWindow.checkTypeDataValue = false;
            return false;
        };
        PopupWindow.checkTypeDataValue = true;
    }
};

//上传合同书成功后调用方法
function uploadSuccessContractBook(file, serverData) {
    try {
        var progress = new FileProgress(file, this.customSettings.progressTarget);
        progress.setComplete();
        progress.setStatus("Complete.");
        progress.toggleCancel(false);

        var innerHtml = '<div class="rel" AttachmentName="' + file.name + '" AttachmentPath="' + serverData + '"><a style="display:inline-block;width:auto !important;max-width:240px;width:240px;overflow:hidden;display:inline-block;text-overflow:ellipsis;vertical-align:middle;white-space:nowrap;" title=' + file.name + ' onclick="OnlyTrust.appendShowContractBookClickEvent(this);">' + file.name + '</a><i onclick="OnlyTrust.delContractBookClickEvent(this);" title="删除" class="red ml4 vm fb cp">×</i><ul style="display:none;overflow:visible;position:absolute;z-index:9999;"><li><img style="width:150px;height:200px;" src="' + other.getFullPhotoPath($("#hidImageDownloadUrl").val(), serverData) + '" /></li><li><i title="关闭" class="cp" style="display:inline-block;width:20px;height:20px;line-height:20px;color:#FFF;text-align:center;background:#000;opacity:.4;filter:alpha(opacity=40);position:absolute;right:0;top:0;" onclick="OnlyTrust.appendHideContractBookClickEvent(this);">×</i></li></ul></div>';
        $("#uploadContractBookDiv").append(innerHtml);

    } catch (ex) {
        this.debug(ex);
    }
}

//保存信息
PopupWindow.btnOnlyTrustConfirm = function (element) {
    PopupWindow.checkTypeData();
    if (!PopupWindow.checkTypeDataValue) {
        return false;
    }
    //if (!confirm("你确定提交保存独家吗？")) {
    //    return false;
    //}
    $.OKCancelDialogAgency('<div class="tc">您确定要提交保存签约吗？</div>', '保存签约', true, function () {
        var attachmentNames = [], attachmentPaths = [], contactKeyIds = [], contactDeptKeyIds = [];
        $("#uploadContractBookDiv div").each(function () {
            attachmentNames.push($(this).attr("AttachmentName"));
            attachmentPaths.push($(this).attr("AttachmentPath"));
        });
        $("#spanAddContacts span").each(function () {
            contactKeyIds.push($(this).find(":hidden[name=contactsKeyId]").val());
            contactDeptKeyIds.push($(this).find(":hidden[name=contactsDepKeyId]").val());
        });

        if ($(element).attr('onlytype') == 'edit') {
            var reqData = {
                "type": "edit",
                "keyId": $('#hidOnlyKeyId').val(),
                "OnlyTrustType": $('#divOnlyTrustType input[name=OnlyTrustType]:checked').val(),
                "PropertyKeyId": propertyKeyId,
                "PersonKeyId": $('#hidUserId').val(),
                "PersonName": $("#btnRegisterPerson").val(),
                "DepartmentKeyId": $("#hidDepartmentKeyId").val(),
                "TrustBookNo": $("#btnTrustBookNo").val(),
                "Deposit": $("#divDeposit input[deposit=deposit]").val(),
                "EffectiveDate": $("#btnPopupTrustsTime1").val(),
                "ExpirationDate": $("#btnPopupTrustsTime2").val(),
                "Comment": $("#txtComment").val(),
                "AttachmentNames": attachmentNames.join(","),
                "AttachmentPaths": attachmentPaths.join(","),
                "ContactKeyIds": contactKeyIds.join(","),
                "ContactDeptKeyIds": contactDeptKeyIds.join(",")
            };
            $.AjaxAgency('/property/property-only-trust-modify-ajax', reqData, function (data) {
                if (data == 'ok') {
                    art.dialog.get('EditOnly').close();
                    GetOnlyTrust.refresh();
                } else {
                    $.OKDialogAgency('<div class="tc">' + data + '</div>');
                }
            });

        } else {
            var propertyKeyId = "";
            if ($('#currentPropertyId').val()) {
                propertyKeyId = $('#currentPropertyId').val();
            } else {
                propertyKeyId = $('#hidPropertyKeyId').val();
            }
            var reqData = {
                "OnlyTrustType": $('#divOnlyTrustType input[name=OnlyTrustType]:checked').val(),
                "PropertyKeyId": propertyKeyId,
                "PersonKeyId": $('#hidUserId').val(),
                "PersonName": $("#btnRegisterPerson").val(),
                "DepartmentKeyId": $("#hidDepartmentKeyId").val(),
                "TrustBookNo": $("#btnTrustBookNo").val(),
                "Deposit": $("#divDeposit input[deposit=deposit]").val(),
                "EffectiveDate": $("#btnPopupTrustsTime1").val(),
                "ExpirationDate": $("#btnPopupTrustsTime2").val(),
                "Comment": $("#txtComment").val(),
                "AttachmentNames": attachmentNames.join(","),
                "AttachmentPaths": attachmentPaths.join(","),
                "ContactKeyIds": contactKeyIds.join(","),
                "ContactDeptKeyIds": contactDeptKeyIds.join(",")
            };
            $.AjaxAgency("/property/property-only-trust-add-ajax", reqData, function (data) {
                if (data.length == 36) {
                    if ("undefined" != typeof GetOnlyTrust) {
                        GetOnlyTrust.refresh();
                    }
                    else {
                        var temdate = new Date();
                        if (reqData.EffectiveDate == temdate.format("yyyy-MM-dd")) {
                            var temdate = new Date();
                            var dujiabtn = $('.icon-fanqian-no[currentProperty=' + $('#currentPropertyId').val() + '] ');
                            $(dujiabtn).attr('class', 'icon-fanqian').attr('addOnlyTrust', 'False');
                            $(dujiabtn).mousemove(pageWarZoneList.showExclusiveDiv);
                            $(dujiabtn).attr("OnlyTrustKeyId", data);
                        }
                    }
                    art.dialog.get('dialogAddExclusive').close();
                } else {
                    $.OKDialogAgency('<div class="tc">' + data + '</div>');
                }
            });
        }
    });
};

PopupWindow.makeUploadBtn = function () {
    //上传合同书
    var settings = {
        flash_url: "/Scripts/PageHelper/swfupload.swf",
        upload_url: $("#uploadPath").val(),
        post_params: {},
        file_size_limit: "2 MB",
        file_types: "*.jpg;*.png",
        file_types_description: "JPG Images",
        file_upload_limit: 20,
        file_queue_limit: 20,
        custom_settings: {
            progressTarget: "fsUploadProgress"
        },
        debug: false,
        // Button settings
        button_image_url: "",
        button_width: "63",
        button_height: "20",
        button_placeholder_id: "uploadSpanContractBook",
        button_cursor: SWFUpload.CURSOR.HAND,
        button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
        button_text: '<span class="btntext">添加附件</span>',
        button_text_style: ".btntext{color:#1f73f0;}",
        //button_text_left_padding: 12,
        //button_text_top_padding: 3,

        // The event handler functions are defined in handlers.js
        file_queued_handler: fileQueued,
        file_queue_error_handler: fileQueueError,
        file_dialog_complete_handler: fileDialogComplete,
        upload_start_handler: uploadStart,
        upload_progress_handler: uploadProgress,
        upload_error_handler: uploadError,
        upload_success_handler: uploadSuccessContractBook,
        upload_complete_handler: uploadComplete,
        queue_complete_handler: queueComplete,	// Queue plugin event

    };
    //上传组件模拟文字链接
    $("#spanSwfupload").hover(function () {
        $(this).find($("#uploadSpanContractBook")).css("opacity", "1");
        $(this).find($(".abs")).hide();
    }, function () {
        $(this).find($("#uploadSpanContractBook")).css("opacity", "0");
        $(this).find($(".abs")).show();
    });
    var swfu = new SWFUpload(settings);
};